package com.event.web.controller.system;

import com.event.common.annotation.Log;
import com.event.common.constant.UserConstants;
import com.event.common.core.controller.BaseController;
import com.event.common.core.domain.AjaxResult;
import com.event.common.core.page.TableDataInfo;
import com.event.common.enums.BusinessType;
import com.event.system.domain.SysEventRoster;
import com.event.system.domain.SysEventSort;
import com.event.system.service.ISysEventRosterService;
import com.event.system.service.ISysEventSortService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/system/eventRoster")
public class SysEventRosterController extends BaseController {

    private String prefix = "system/eventRoster";

    @Autowired
    private ISysEventRosterService sysEventRosterService;

    @Autowired
    private ISysEventSortService sysEventSortService;

    @RequiresPermissions("system:eventRoster:view")
    @GetMapping()
    public String eventsort(ModelMap mmap)
    {
        mmap.put("eventSort", sysEventSortService.selectEventSortAll());
        return prefix + "/eventRoster";
    }

    @RequiresPermissions("system:eventRoster:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysEventRoster sysEventRoster)
    {
        startPage();
        List<SysEventRoster> list = sysEventRosterService.selectSysEventRosterList(sysEventRoster);
        return getDataTable(list);
    }

    /**
     * 新增展会名单
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("eventSort", sysEventSortService.selectEventSortAll());
        return prefix + "/add";
    }

    /**
     * 新增保存展会名单
     */
    @RequiresPermissions("system:eventRoster:add")
    @Log(title = "展会名单管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysEventRoster sysEventRoster)
    {
        if (UserConstants.ROLE_NAME_NOT_UNIQUE.equals(sysEventRosterService.checkEventNameUnique(sysEventRoster)))
        {
            return error("新增展会名单'" + sysEventRoster.getEventName() + "'失败，展会名单名称已存在");
        }
        return toAjax(sysEventRosterService.add(sysEventRoster));
    }

    /**
     * 校验展会名单名称
     */
    @PostMapping("/checkEventNameUnique")
    @ResponseBody
    public String checkEventNameUnique(SysEventRoster sysEventRoster)
    {
        return sysEventRosterService.checkEventNameUnique(sysEventRoster);
    }

    /**
     * 修改展会名单
     */
    @RequiresPermissions("system:eventRoster:edit")
    @GetMapping("/edit/{rosterId}")
    public String edit(@PathVariable("rosterId") Long rosterId, ModelMap mmap)
    {
        mmap.put("eventSort", sysEventSortService.selectEventSortAll());
        mmap.put("eventRoster", sysEventRosterService.selectSortEventRosterBySortId(rosterId));
        return prefix + "/edit";
    }

    /**
     * 修改保存展会名单
     */
    @RequiresPermissions("system:eventRoster:edit")
    @Log(title = "展会名单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysEventRoster sysEventRoster)
    {
        if (UserConstants.ROLE_NAME_NOT_UNIQUE.equals(sysEventRosterService.checkEventNameUnique(sysEventRoster)))
        {
            return error("修改展会名单'" + sysEventRoster.getEventName() + "'失败，展会名单名称已存在");
        }
        return toAjax(sysEventRosterService.update(sysEventRoster));
    }

    /**
     * 删除展会名单
     */
    @RequiresPermissions("system:eventRoster:remove")
    @Log(title = "展会名单管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysEventRosterService.deleteSysEventRosterBySortId(ids));
    }
}
